package com.bdth.bdthiss.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.bdthiss.entity.Category;
import com.bdth.bdthiss.entity.Excelgoods;
import com.bdth.bdthiss.entity.User;
import com.bdth.bdthiss.service.CategoryService;
import com.bdth.bdthiss.vo.CategoryVo;
import com.bdth.bdthiss.vo.MessageResult;
import com.bdth.bdthiss.vo.PageVo;
import com.bdth.bdthiss.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 产品分类表 前端控制器
 * </p>
 *
 * @author LGP
 * @since 2020-04-06
 */
@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value = "top")
    public String top() {
        return "category/top";
    }

    @RequestMapping(value = "index")
    public String index() {
        return "category/index";
    }

    @RequestMapping(value = "form")
    public String form() {
        return "category/form";
    }

    @RequestMapping(value = "sub")
    public String sub() {
        return "category/sub";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Category> getPageList(HttpServletRequest request, Pagination<Category> pagination,String type,String name){
        Map<String, Object> query = new HashMap<>();
        query.put("type",type);
        if(name!=null && !"".equals(name))
            query.put("name",name);
        Page<Category> items =  categoryService.listPages(pagination,query);
        PageVo<Category> pageVo = new PageVo<>(items.getRecords(),items.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "search")
    public String search() {
        return "category/search";
    }

    @RequestMapping(value = "subsearch")
    public String subsearch() {
        return "category/subsearch";
    }

    @RequestMapping(value = "topsearch")
    public String topsearch() {
        return "category/topsearch";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(CategoryVo param){
        Category bean = new Category();
        bean.setName(param.getName());
        bean.setHash(0);
        bean.setParentId(param.getTypeid());
        bean.setVisibility(1);
        categoryService.insert(bean);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(CategoryVo param){
        Category bean = new Category();
        bean.setName(param.getName());
        bean.setId(param.getId());
        bean.setParentId(param.getTypeid());
        categoryService.updateById(bean);
        return MessageResult.getSuccess("修改数据成功");
    }
}

